home *** CD-ROM | disk | FTP | other *** search
/ The 640 MEG Shareware Studio 2 / The 640 Meg Shareware Studio CD-ROM Volume II (Data Express)(1993).ISO / clang / ctask.zip / TSKMSG.C < prev    next >
C/C++ Source or Header  |  1988-07-01  |  3KB  |  155 lines

  1. /*
  2.    TSKMSG.C - CTask - Message handling routines.
  3.  
  4.    Public Domain Software written by
  5.       Thomas Wagner
  6.       Patschkauer Weg 31
  7.       D-1000 Berlin 33
  8.       West Germany
  9. */
  10.  
  11. #include <stdio.h>
  12.  
  13. #include "tsk.h"
  14. #include "tsklocal.h"
  15.  
  16.  
  17. /*
  18.    create_mailbox - initialises mailbox.
  19. */
  20.  
  21. mailboxptr far create_mailbox (mailboxptr box
  22. #if (TSK_NAMEPAR)
  23.                               ,byteptr name
  24. #endif
  25.                               )
  26. {
  27. #if (TSK_DYNAMIC)
  28.    if (box == NULL)
  29.       {
  30.       if ((box = tsk_alloc (sizeof (mailbox))) == NULL)
  31.          return NULL;
  32.       box->flags = F_TEMP;
  33.       }
  34.    else
  35.       box->flags = 0;
  36. #endif
  37.  
  38.    box->waiting = NULL;
  39.    box->mail_first = box->mail_last = NULL;
  40.  
  41. #if (TSK_NAMED)
  42.    tsk_add_name (&box->name, name, TYP_MAILBOX, box);
  43. #endif
  44.  
  45.    return box;
  46. }
  47.  
  48.  
  49. /*
  50.    delete_mailbox - kills all processes waiting for mail
  51. */
  52.  
  53. void far delete_mailbox (mailboxptr box)
  54. {
  55.    CRITICAL;
  56.  
  57.    C_ENTER;
  58.    tsk_kill_queue (&(box->waiting));
  59.    box->mail_first = box->mail_last = NULL;
  60.    C_LEAVE;
  61.  
  62. #if (TSK_NAMED)
  63.    tsk_del_name (&box->name);
  64. #endif
  65.  
  66. #if (TSK_DYNAMIC)
  67.    if (box->flags & F_TEMP)
  68.       tsk_free (box);
  69. #endif
  70. }
  71.  
  72.  
  73. /*
  74.    wait_mail - Wait until mail arrives. If there is mail in the box on
  75.                entry, the first mail block is assigned to the caller,
  76.                and the task continues to run.
  77. */
  78.  
  79. farptr far wait_mail (mailboxptr box, dword timeout)
  80. {
  81.    msgptr msg;
  82.    CRITICAL;
  83.  
  84.    C_ENTER;
  85.    if ((msg = box->mail_first) != NULL)
  86.       {
  87.       if ((box->mail_first = msg->next) == NULL)
  88.          box->mail_last = NULL;
  89.       C_LEAVE;
  90.       return msg;
  91.       }
  92.  
  93.    tsk_wait (&box->waiting, timeout);
  94.    return tsk_current->retptr;
  95. }
  96.  
  97. /*
  98.    c_wait_mail - If there is mail in the box on entry, the first mail 
  99.                  block is assigned to the caller, else an error is returned.
  100. */
  101.  
  102. farptr far c_wait_mail (mailboxptr box)
  103. {
  104.    msgptr msg;
  105.    CRITICAL;
  106.  
  107.    C_ENTER;
  108.    if ((msg = box->mail_first) != NULL)
  109.       if ((box->mail_first = msg->next) == NULL)
  110.          box->mail_last = NULL;
  111.    C_LEAVE;
  112.    return msg;
  113. }
  114.  
  115.  
  116. /*
  117.    send_mail - Send a mail block to a mailbox. If there are tasks waiting
  118.                for mail, the first waiting task is assigned the block and
  119.                is made eligible.
  120. */
  121.  
  122. void far send_mail (mailboxptr box, farptr msg)
  123. {
  124.    tcbptr curr;
  125.    CRITICAL;
  126.  
  127.    C_ENTER;
  128.    if ((curr = box->waiting) == NULL)
  129.       {
  130.       if (box->mail_first == NULL)
  131.          box->mail_first = (msgptr)msg;
  132.       else
  133.          box->mail_last->next = (msgptr)msg;
  134.       ((msgptr)msg)->next = NULL;
  135.       box->mail_last = (msgptr)msg;
  136.       C_LEAVE;
  137.       return;
  138.       }
  139.    box->waiting = tsk_runable (curr);
  140.    curr->retptr = msg;
  141.    C_LEAVE;
  142. }
  143.  
  144.  
  145. /*
  146.    check_mailbox - returns TRUE if there is mail in the box.
  147. */
  148.  
  149. int far check_mailbox (mailboxptr box)
  150. {
  151.    return box->mail_first != NULL;
  152. }
  153.  
  154.  
  155.